<template>
  <div class="container">
    <el-card>
      <img class="logo" src="@/assets/images/logo.png" alt="">
      <!-- 表单 -->
      <el-form ref="formRef" :rules="rules" :model="form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-checkbox :value="true">我已阅读并同意「用户协议」和「隐私条款」</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="login()" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import auth from '@/utils/auth.js'
export default {
  name: 'LoginPage',
  data () {
    const checkMobile = (rule, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        callback()
      } else {
        callback(new Error('手机号格式不对'))
      }
    }
    return {
      // 数据
      form: {
        mobile: '19124642735',
        code: '246810'
      },
      // 规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '验证码长度为6', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          try {
            const res = await this.$http.post('/v1_0/authorizations', this.form)
            console.log(res)
            // localStorage.setItem('geek-client-lx', res.data.data.token)
            auth.setToken(res?.data.data.token)
            this.$router.push('/')
            this.$message.success('登录成功')
          } catch (e) {
            this.$message.error(e.response.data.message || '登录失败')
          }
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.container{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url('@/assets/images/login.png');

  .el-card{
    width: 440px;
    height: 380px;
    position: absolute;
    left: 50%;
    top: 50%;
    // 设置元素的位置，使其相对于其正常位置向左和向上各移动50%
    // 这通常用于居中元素，通过将其左上角定位到其父元素的中心点
    transform: translate(-50%,-50%);
    box-shadow: 0 0 50px rgba(0,0,0,0.1);

    .logo{
      width: 200px;
    // 设置元素显示为块级元素，并居中显示，下边距为20px
      display: block;
      margin: 0 auto 20px;
    }
    .el-form{
      padding: 0 20px;
      .el-button{
        width: 100%;
      }
    }
  }
}
</style>
